//
//  Config.swift
//  guard
//
//  Created by linus on 2021/11/20.
//

import UIKit

public class Config: NSObject {
    public typealias ConfigCallback = (Config?) -> Void
    
    private var _userPoolId: String?
    public var userPoolId: String? {
        get { _userPoolId }
        set { _userPoolId = newValue }
    }
    
    private var _identifier: String?
    public var identifier: String? {
        get { _identifier }
        set { _identifier = newValue }
    }
    
    private var _name: String?
    public var name: String? {
        get { _name }
        set { _name = newValue }
    }
    
    private var _userpoolLogo: String?
    public var userpoolLogo: String? {
        get { _userpoolLogo }
        set { _userpoolLogo = newValue }
    }

    private var _enabledLoginMethods: Array<String>?
    public var enabledLoginMethods: Array<String>? {
        get { _enabledLoginMethods }
        set { _enabledLoginMethods = newValue }
    }
    
    private var _loginTabList: Array<String>?
    public var loginTabList: Array<String>? {
        get { _loginTabList }
        set { _loginTabList = newValue }
    }
    
    private var _defaultLoginMethod: String?
    public var defaultLoginMethod: String? {
        get { _defaultLoginMethod }
        set { _defaultLoginMethod = newValue }
    }
    
    private var _registerTabList: Array<String>?
    public var registerTabList: Array<String>? {
        get { _registerTabList }
        set { _registerTabList = newValue }
    }
    
    private var _defaultRegisterMethod: String?
    public var defaultRegisterMethod: String? {
        get { _defaultRegisterMethod }
        set { _defaultRegisterMethod = newValue }
    }
    
    private var _verifyCodeLength: Int? = 6
    public var verifyCodeLength: Int? {
        get { _verifyCodeLength }
        set { _verifyCodeLength = newValue }
    }
    
    private var _socialConfigs: Array<SocialConfig>?
    public var socialConfigs: Array<SocialConfig>? {
        get { _socialConfigs }
        set { _socialConfigs = newValue }
    }
    
    private var _agreements: Array<Agreement>?
    public var agreements: Array<Agreement>? {
        get { _agreements }
        set { _agreements = newValue }
    }
    
    private var _passwordStrength: Int?
    public var passwordStrength: Int? {
        get { _passwordStrength }
        set { _passwordStrength = newValue }
    }
    
    public static func parse(data: NSDictionary) -> Config? {
        let config = Config()
        
        config.userPoolId = data.object(forKey: "userPoolId") as? String
        config.identifier = data.object(forKey: "identifier") as? String
        config.name = data.object(forKey: "name") as? String
        config.userpoolLogo = data.object(forKey: "userpoolLogo") as? String
        config.verifyCodeLength = data.object(forKey: "verifyCodeLength") as? Int
        config.passwordStrength = data.object(forKey: "passwordStrength") as? Int
        
        if let loginTabs = data.object(forKey: "loginTabs") as? NSDictionary {
            config.defaultLoginMethod = loginTabs.object(forKey: "default") as? String
            if let loginTabList = loginTabs.object(forKey: "list") as? NSArray {
                config.loginTabList = toStringList(loginTabList)
            }
        }
        
        if let registerTabs = data.object(forKey: "registerTabs") as? NSDictionary {
            config.defaultRegisterMethod = registerTabs.object(forKey: "default") as? String
            if let registerTabList = registerTabs.object(forKey: "list") as? NSArray {
                config.registerTabList = toStringList(registerTabList)
            }
        }
        
        if let passwordTabConfig = data.object(forKey: "passwordTabConfig") as? NSDictionary {
            if let enableLoginMethod = passwordTabConfig.object(forKey: "enabledLoginMethods") as? NSArray {
                config.enabledLoginMethods = toStringList(enableLoginMethod)
            }
        }
        
        if let socialConnections = data.object(forKey: "socialConnections") as? NSArray {
            config.socialConfigs = toSocialList(socialConnections)
        }
        
        if let agreements = data.object(forKey: "agreements") as? NSArray {
            config.agreements = toAgreementList(agreements)
        }
        
        return config
    }
    
    private static func toStringList(_ array: NSArray) -> Array<String> {
        var list = Array<String>()
        for item in array {
            if let s_item = item as? String {
                list.append(s_item)
            }
        }
        return list
    }
    
    private static func toSocialList(_ array: NSArray) -> Array<SocialConfig> {
        var list = Array<SocialConfig>()
        for item in array {
            if let d_item = item as? NSDictionary {
                let config = SocialConfig()
                config.id = d_item.object(forKey: "id") as? String
                config.provider = d_item.object(forKey: "provider") as? String
                list.append(config)
            }
        }
        return list
    }
    private static func toAgreementList(_ array: NSArray) -> Array<Agreement> {
        var list = Array<Agreement>()
        for item in array {
            if let d_item = item as? NSDictionary {
                let config = Agreement()
                config.title = d_item.object(forKey: "title") as? String
                config.lang = d_item.object(forKey: "lang") as? String
                config.isRequired = d_item.object(forKey: "required") as? Bool
                list.append(config)
            }
        }
        return list
    }
}
